/**
 * 
 */
/**
 * @author shanghaitao
 *
 */
package com.starbaba.system.userofgroup.action;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.google.gson.JsonObject;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.common.ResultJson;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.system.usergroup.model.UsergroupComidVO;
import com.starbaba.system.userofgroup.model.UserOfGroupVO;
import com.starbaba.system.userofgroup.model.UserOfGroupModel;
import com.starbaba.system.userofgroup.service.UserOfGroupService;

@SuppressWarnings({ "serial", "rawtypes" })
public class UserOfGroupAction extends BaseAction implements ModelDriven {
	@Resource(name="UserOfGroupServiceImpl")
	private UserOfGroupService service;
	
	private UserOfGroupModel model;
	
	/**
	 * 
	 * @Title: userOfGroupList
	 * @Description: 【用户列表】
	 * @param
	 * @return 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	@SuppressWarnings("unchecked")
    public void userOfGroupList(){
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(
				Struts2Utils.getRequest(), paramsMap);
		//type为1--查询全部(备用)
		String type = (String) paramsMap.get("type");
		String userid = (String) paramsMap.get("userid");
		if(null != userid){
			model.setUserid(userid);
		}
		parsePageRequest();
		try {
			PageResponse pageResponse = new PageResponse();
			Integer startPoint = pageRequest.getIDisplayStart();
			Integer displayLength = pageRequest.getIDisplayLength();
			if(null == type){
					startPoint = 0;
					displayLength = 100000;
			}
			List list = service.getDataList(startPoint , displayLength,model,pageResponse,getService());
			pageResponse.setAaData(list);
			pageResponse.setSEcho(pageRequest.getSEcho());
			String jsonStr = JsonUtils.toJson(pageResponse, null, true, null, false);
			sendJsonMessage(jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//添加用户到组/删除用户
	public void userOfGroupChange(){
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(
				Struts2Utils.getRequest(), paramsMap);
		//type为1--新增,为0--按groupid删除,2为删除所有后添加为管理员
		String type = (String) paramsMap.get("type");
		String groupid = (String) paramsMap.get("groupid");
		String comid = (String) paramsMap.get("comid");
		String creator = (String) paramsMap.get("creator");
		String userid = (String) paramsMap.get("userid");
		try {
			UserOfGroupVO entity = new UserOfGroupVO();	
			entity.setGroupid(Long.valueOf(groupid));
			entity.setUserid(userid);
			entity.setCreator(creator);
			entity.setCreatetime(new Date());
			if(Integer.valueOf(type) == 1){
				Object ret = this.getService().doCreate(UserOfGroupVO.class, entity);
				if (null == ret) {
					sendErrMsg("新增失败");	
				} else {
					UsergroupComidVO entity1 = new UsergroupComidVO();	
					entity1.setGroupid(Long.valueOf(groupid));
					entity1.setComid(comid);
					Object ret1 = this.getService().doCreate(UsergroupComidVO.class, entity1);
					if (null == ret1) {
						sendErrMsg("新增失败");	
					}else{
						JsonObject result = new JsonObject();
						ResultJson resultJs = new ResultJson();
						resultJs.msg = "添加成功";
						result.add("result", resultJs.toJSON());
						sendJsonMessage(result.toString());	
					}	
				}	
			}else if(Integer.valueOf(type) == 0){
				Object ret = getService().executeHQLUpdate("delete UserOfGroupVO vo where vo.userid = ? and vo.groupid = ?", new Object[]{userid, Long.valueOf(groupid)});
				if (null == ret) {
					sendErrMsg("删除失败");	
				} else {
					Object ret1 = getService().executeHQLUpdate("delete UsergroupComidVO vo1 where vo1.comid = ? and vo1.groupid = ?", new Object[]{comid, Long.valueOf(groupid)});
					if (null == ret1) {
						sendErrMsg("删除失败");	
					} else {
						JsonObject result = new JsonObject();
						ResultJson resultJs = new ResultJson();
						resultJs.msg = "删除成功";
						result.add("result", resultJs.toJSON());
						sendJsonMessage(result.toString());	
					}	
				}	
			}else if(Integer.valueOf(type) == 2){
				Object ret = getService().executeHQLUpdate("delete UserOfGroupVO vo where vo.userid = ? ", new Object[]{userid});
				if (null == ret) {
					sendErrMsg("删除(全部)失败");	
				} else {
					Object ret1 = getService().executeHQLUpdate("delete UsergroupComidVO vo1 where vo1.comid = ? ", new Object[]{comid});
					if (null == ret1) {
						sendErrMsg("删除(全部)失败");	
					} else {
						JsonObject result = new JsonObject();
						ResultJson resultJs = new ResultJson();
						resultJs.msg = "删除(全部)成功";
						result.add("result", resultJs.toJSON());
						sendJsonMessage(result.toString());	
					}	
				}	
			}
								
		} catch (Exception e) {
			sendErrMsg(e.getMessage());
		}
	}
	


	@Override
	public Object getModel() {
		if(model == null)
		{
			model = new UserOfGroupModel();
		}
		return model;
	}
	
	private void sendErrMsg(String msg) {
		JsonObject result = new JsonObject();
		ResultJson resultJs = new ResultJson();
		resultJs.msg = msg;
		result.add("result", resultJs.toJSON());
		try {
			sendJsonMessage(result.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}